package cn.sinata.wanglin.village.fragment;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.xilada.xldutils.fragment.RecyclerViewFragment;
import com.xilada.xldutils.utils.DensityUtil;
import com.xilada.xldutils.utils.Utils;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import cn.sinata.wanglin.MainActivity;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.adapter.VillageActiveAdapter;
import cn.sinata.wanglin.mine.fragment.MineFragment;
import cn.sinata.wanglin.model.Active;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.village.activity.AllUserInfoActivity;

/**
 * description：个人信息--小区活动->活动
 * Created by kaihongwu on 2016/12/06 上午 9:45
 */
public class OtheruserActiveFragment extends RecyclerViewFragment implements AppBarLayout.OnOffsetChangedListener{
    private List<Active> mActives=new ArrayList<Active>();
    private VillageActiveAdapter mVillageActiveAdapter;
    private int pageNo = 0;
    private  String userid=null;
    public static OtheruserActiveFragment newInstance(String userid){
        OtheruserActiveFragment mOtheruserActiveFragment=new OtheruserActiveFragment();
        Bundle bundle = new Bundle();
        bundle.putString("userid",userid);
        mOtheruserActiveFragment.setArguments(bundle);
        return mOtheruserActiveFragment;
    }
    @Override
    protected RecyclerView.Adapter setAdapter() {

        if(mVillageActiveAdapter==null){
            if (getArguments()!=null){
                userid = getArguments().getString("userid");
            }
            HorizontalDividerItemDecoration itemDecoration = new HorizontalDividerItemDecoration.Builder(context)
                    .size(DensityUtil.dip2px(context,10))
                    .color(ContextCompat.getColor(context, R.color.dividing_line_color_wanglin))
                    .build();
            setItemDecoration(itemDecoration);
            mVillageActiveAdapter = new VillageActiveAdapter(mActives,getActivity(),0);
        }

        return mVillageActiveAdapter;
    }


    @Override
    protected void pullDownRefresh() {
        super.pullDownRefresh();
        pageNo = 0;
        getData();
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        pageNo++;
        getData();
    }

    @Override
    protected void onFirstVisibleToUser() {
        Utils.systemErr("---小区-->小区动态-->活动---");
        if(getArguments()!=null){
            userid=getArguments().getString("userid");
        }

        setEmptyViewClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
                getData();
            }
        });

        addOnOffsetChangeListener();

        getData();
    }

    @Override
    protected boolean useDefaultEmptyView() {
        return false;
    }

    private void getData() {
        HttpManager.getMyActivity(userid,pageNo).subscribe(new ResultDataSubscriber<ArrayList<Active>>(this) {
            @Override
            public void onSuccess(String msg, ArrayList<Active> data) {
                if (pageNo == 0) {
                    mActives.clear();
                }
                if (data.size() == 0) {
                    if (pageNo == 0) {
                        setLoadMoreText("暂无数据");
                    } else {
                        pageNo --;
                        setLoadMoreText("没有更多了");
                    }
                }
                mActives.addAll(data);
                mVillageActiveAdapter.notifyDataSetChanged();
            }
        });

    }

    @Override
    protected void onVisibleToUser() {
        addOnOffsetChangeListener();
    }

    @Override
    protected void onInvisibleToUser() {
        removeOnOffsetChangeListener();
    }

    void addOnOffsetChangeListener(){
        if (getActivity() instanceof AllUserInfoActivity) {
            ((AllUserInfoActivity) getActivity()).addOnOffsetChangedListener(this);
        } else if (getParentFragment()!=null && getParentFragment() instanceof MineFragment) {
            ((MineFragment) getParentFragment()).addOnOffsetChangedListener(this);
        }
    }

    void removeOnOffsetChangeListener(){
        if (getActivity() instanceof AllUserInfoActivity) {
            ((AllUserInfoActivity) getActivity()).removeOnOffsetChangedListener(this);
        }else if (getParentFragment()!=null && getParentFragment() instanceof MineFragment) {
            ((MineFragment) getParentFragment()).removeOnOffsetChangedListener(this);
        }
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (verticalOffset >= 0) {
            mSwipeRefreshLayout.setEnabled(true);
        } else {
            mSwipeRefreshLayout.setEnabled(false);
        }
    }
}
